package com.hw.protocol.tool.model;

import lombok.Data;

import java.io.Serializable;

/**
 * @author :lihy
 * @date :2020/12/8 17:21
 * description : 统一返回包装类
 **/
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 952598541894571804L;

    private int code;

    private String message;

    private T data;

    public Result() {
    }

    public Result(int code) {
        this.code = code;
    }

    private Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> status(boolean flag) {
        return flag ? success() : error();
    }

    public static <T> Result<T> success() {
        return info(200, "操作成功");
    }

    public static <T> Result<T> success(String message, T t) {
        return info(200, message, t);
    }

    public static <T> Result<T> success(T t) {
        return info(200, "", t);
    }

    public static <T> Result<T> error() {
        return info(400, "操作异常");
    }

    public static <T> Result<T> error(String message) {
        return info(400, message);
    }

    public static <T> Result<T> error(int code, String message) {
        return info(code, message);
    }


    public static <T> Result<T> info(int code, String message) {
        return new Result(code, message);
    }

    public static <T> Result<T> info(int code, String message, T t) {
        return new Result(code, message, t);
    }

    public static <T> Result<T> data(T t) {
        return new Result(200, "", t);
    }

    public static <T> Result<T> data(int code, T t) {
        return new Result(code, "", t);
    }

    public static <T> Result<T> data(int code, String message, T t) {
        return new Result(code, message, t);
    }
}
